import Storage from 'react-native-storage';
import AsyncStorage from '@react-native-async-storage/async-storage';

const Collect = 'Collect'
export const loadAllCollect=()=> {
    
    return storage.getAllDataForKey(Collect).then(collects => {
        // 如果找到数据，则在then方法中返回
        console.log('loadAllCollect', collects);
        return collects;
    }).catch(err => {
        // 如果没有找到数据且没有sync方法，
        // 或者有其他异常，则在catch中返回
        console.warn('读取数据失败loadAllCollect', err.message);
        switch (err.name) {
            case 'NotFoundError':
                // TODO;
                break;
            case 'ExpiredError':
                // TODO
                break;
        }
    })
}

export const saveCollect = (collect) => {
    storage.save({
        key: Collect,
        id: collect.xm.replace('_', ''),
        data: collect
    })
}

export const removeCollect = (id) => {
    storage.remove({
        key: Collect,
        id: id.replace('_', '')
    });
}



const CONFIG = 'config';

export const saveAppConfig = (appConfig) =>{
    try{
        if (appConfig !== null && appConfig !== undefined) {
            storage.save({
                key: CONFIG,
                data: appConfig
            })
        }
    }catch(e){
        console.warn('saveAppConfig func',e.message)
    }
}

export const loadAppConfig=()=> {
    try{
        return storage.load({
            key: CONFIG
        }).then(config => {
            // 如果找到数据，则在then方法中返回
            return config;
        }).catch(err => {
            // 如果没有找到数据且没有sync方法，
            // 或者有其他异常，则在catch中返回
            console.warn('读取数据失败loadAppConfig', err.message);
            switch (err.name) {
                case 'NotFoundError':
                    // TODO;
                    break;
                case 'ExpiredError':
                    // TODO
                    break;
            }
            return null
        })
    }catch(e){
        console.log('loadAppConfig func', e.message)
    }
}

const NAMELIST = 'NAMELIST';

export const saveName = (name) =>{
    try{
        storage.save({
            key: NAMELIST,
            data: name
        })
    }catch(e){
        console.warn('saveAppConfig func',e.message)
    }
}

export const loadNames=()=> {
    try{
        return storage.load({
            key: NAMELIST
        }).then(nameList => {
            // 如果找到数据，则在then方法中返回
            return nameList;
        }).catch(err => {
            // 如果没有找到数据且没有sync方法，
            // 或者有其他异常，则在catch中返回
            console.warn('读取数据失败loadNames', err.message);
            switch (err.name) {
                case 'NotFoundError':
                    // TODO;
                    break;
                case 'ExpiredError':
                    // TODO
                    break;
            }
            return []
        })
    }catch(e){
        console.log('loadAppConfig func', e.message)
    }
}

const storage = new Storage({
    // maximum capacity, default 1000 key-ids
    size: 1000,

    // Use AsyncStorage for RN apps, or window.localStorage for web apps.
    // If storageBackend is not set, data will be lost after reload.
    storageBackend: AsyncStorage, // for web: window.localStorage

    // expire time, default: 1 day (1000 * 3600 * 24 milliseconds).
    // can be null, which means never expire.
    defaultExpires: null,

    // cache data in the memory. default is true.
    enableCache: true,

    // if data was not found in storage or expired data was found,
    // the corresponding sync method will be invoked returning
    // the latest data.
    sync: {
        // we'll talk about the details later.
    }
});

